<?php
global $_W, $_GPC;
load()->func('tpl');
$op =empty($_GPC['op']) ? 'display' : $_GPC['op'];
if($op == 'display'){
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $activity = pdo_fetchall('select * from ' . tablename('franchise_activity') . "where  uniacid=:uniacid ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize, array(':uniacid' => $_W['uniacid']));
    foreach ($activity as $k => $v) {
        $activity[$k]['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
    }
    $total = pdo_fetchcolumn('select * from ' . tablename('franchise_activity') . "where uniacid=:uniacid ORDER BY id DESC", array(':uniacid' => $_W['uniacid']));
    $pager = pagination($total, $pindex, $psize);
    include $this->template('web/franchise/activity');
    exit;
}
if ($op == 'status_set') {
    $id = intval($_GPC['id']);
    $info = pdo_fetch('select * from ' . tablename('franchise_activity') . " where uniacid = '{$_W['uniacid']}' and id='{$id}' ");
    if (!empty($info)) {
        $status = $info['status'] == 1 ? '0' : '1';
        pdo_update("franchise_activity", array('status' => $status), array("id" => $id, "uniacid" => $_W['uniacid']));
        die(json_encode(array("result" => 1, 'status' => $status)));
    } else {
        die(json_encode(array("result" => 0)));
    }
}
if($op == 'edit'){
    $id = intval($_GPC['id']);
    if (!empty($id)) {
        $activity = pdo_fetch('SELECT * FROM ' . tablename('franchise_activity') . ' WHERE id=:id and uniacid=:uniacid ', array(':uniacid' => $_W['uniacid'], ':id' => $id));
        $activity['createtime'] = date('Y-m-d H:i', $activity['createtime']);
    }
    if (array_key_exists('submit', $_GPC)) {
        empty($_GPC['title']) && message('请填写活动名称');
        empty($_GPC['image']) && message('请选择活动图片');
        empty($_GPC['about']) && message('请填写活动简介');
        empty($_GPC['description']) && message('请填写活动详情');
        $data['uniacid'] = $_W['uniacid'];
        $data['title'] = $_GPC['title'];
        $data['image'] = $_GPC['image'];
        $data['about'] = $_GPC['about'];
        $data['createtime'] = time();
        $data['status'] = $_GPC['status'];
        $data['description'] = htmlspecialchars_decode($_GPC['description']);
        if (!empty($id)) {
            pdo_update('franchise_activity', $data, array('uniacid' => $_W['uniacid'], 'id' => $id));
        } else {
            pdo_insert('franchise_activity', $data);
        }
        message('活动保存成功', $this->createWebUrl('franchise/activity'), 'success');
    }
    include $this->template('web/franchise/activity');
    exit;
}
if($op == 'join'){
    $id = intval($_GPC['id']);
    if(empty($id)){
    message('开业活动不存在!', $this->createWebUrl('franchise/activity'), 'error');
    }
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $activity = pdo_fetchall('select j.*,m.avatar,m.nickname,o.name,o.shop_name,o.shop_address,o.mobile from ' . tablename('ewei_shop_member') . "as m left join ".tablename('franchise_apply_order')."as o on m.openid=o.openid left join ".tablename('franchise_activity_join')."as j on j.openid=m.openid where  j.uniacid=:uniacid and j.activity_id=:activity_id and o.status=2 ORDER BY j.id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize, array(':uniacid' => $_W['uniacid'],':activity_id'=>$id));
    include $this->template('web/franchise/activity');
    exit;
}